#!/usr/bin/python

"""
Structures for multiplying numbers together.
"""

import sys

from utils import *
from structure import *
from logger import logger

class MultiplierError(Exception):
    """
    handling class for multiplier exceptions
    """
    
    def __init__(self, msg):
        """
        initialize the class
        """
        logger.error("multiplier: %s" % msg)

class Multiplier(Structure):
    """
    Defines a structure that finds the product of two inputs
    """
    
    def __init__(self, dim = MULTIPLIER_DIMENSION, type = 'x'):
        """
        initialize a multiplier structure
        """
        Structure.__init__(self, dim, 1, type)
        
    def compute(self):
        """
        use the inputs to calculate an output
        """
        if len(self.inputs) >= 2:
            total = int(self.inputs[0])
            for input in self.inputs[1:]:
                total *= int(input)
            self.result = str(total)
        else:
            raise MultiplierError("need two or more inputs")

class Tcm(Multiplier):
    """
    a two's complement multiplier
    """
    
    def __init__(self, dim = MULTIPLIER_DIMENSION):
        """
        initialize the multiplier
        """
        Multiplier.__init__(self, dim, 'A')
                
        #first in each row is type 'D', diagonal cells are type 'C'
        for row in range(dim):
            self.cellfield[row][0].set_type('D')
            self.cellfield[row][row].set_type('C')

        #last row is type 'F'
        for entry in self.cellfield[-1]:
            entry.set_type('F')
            
        #upper left cell is type 'B'
        self.cellfield[0][0].set_type('B')
        
        #lower left cell is type 'H'
        self.cellfield[-1][0].set_type('H')

        #lower right cell is type 'E'
        self.cellfield[-1][-1].set_type('E')
        
class Usm(Multiplier):
    """
    an unsigned multiplier
    """
    
    def __init__(self, dim = MULTIPLIER_DIMENSION):
        """
        initialize the multiplier
        """
        Multiplier.__init__(self, dim, 'A')
